package com.example.financeapi.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.Random;
import java.util.UUID;

/**
 * @author Alvin
 */
public class KeyUtil {

    /**
     * 生成唯一的主键
     * 格式: 时间+随机数
     * @return
     */
    public static synchronized String genUniqueKey() {
        Random random = new Random();
        Integer number = random.nextInt(900000) + 100000;
        return System.currentTimeMillis() + String.valueOf(number);
    }

    /**
     * 生成UUID
     * @param prefix 前缀
     * @return
     */
    public static String generateUUID(String prefix){
        String uuid = (UUID.randomUUID().toString()).replaceAll("-","");
        if (!StringUtils.isEmpty(prefix)) {
            uuid = prefix + "-" + uuid;
        }
        return uuid;
    }
}
